<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px" class="queryForms">
			<el-form-item label="检定日期" prop="lastAppraisalDate" label-width="100px">
				<el-date-picker v-model="queryParams.lastAppraisalDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择检定日期"> </el-date-picker>
			</el-form-item>
			<el-form-item label="设备名称" prop="deviceName">
				<el-input v-model="queryParams.deviceName" placeholder="请输入设备名称" clearable @keyup.enter.native="handleQuery" maxlength="20" show-word-limit />
			</el-form-item>
			<el-form-item label="检定周期" prop="periodValidity">
				<!-- <el-select v-model="queryParams.periodValidity" placeholder="请选择检定周期">
					<el-option v-for="dict in dict.type.period_validity" :key="dict.value" :label="dict.label" :value="dict.value"> </el-option>
				</el-select> -->
				<el-select v-model="queryParams.periodValidity">
					<el-option v-for="item in timelist" :key="item.id" :value="item.id" :label="item.name"> </el-option>
				</el-select>
			</el-form-item>
			<!-- <el-form-item label="检定状态" prop="authenticateStatus">
				<el-select v-model="queryParams.authenticateStatus" placeholder="请选择检定状态">
					<el-option v-for="dict in dict.type.authenticate_status" :key="dict.value" :label="dict.label" :value="dict.value"> </el-option>
				</el-select>
			</el-form-item> -->
			<el-form-item label="设备编号" prop="deviceNumber">
				<el-input v-model="queryParams.deviceNumber" placeholder="请输入设备编号" clearable @keyup.enter.native="handleQuery" maxlength="20" show-word-limit />
			</el-form-item>
			<el-form-item label="完好状态" prop="intactState">
				<el-select v-model="queryParams.intactState" placeholder="请选择完好状态">
					<el-option v-for="dict in dict.type.measure_intact_state" :key="dict.value" :label="dict.label" :value="dict.value"> </el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="购置日期" prop="purchaseTime">
				<el-date-picker v-model="queryParams.purchaseTime" type="date" placeholder="选择日期" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" size="mini" @click="handleAdd" v-hasPermi="['quality:qmeasure:plan']">检定计划</el-button>
			</el-col>
			<CreateForm :ids="ids" formtype="t_quality_qmeasure" />

			<right-toolbar menuNames="qmeasure40" @changeTableData="changeTableData" :showSearch.sync="showSearch" @queryTable="getList" :columns.sync="columns" perm="quality:qmeasure:index"></right-toolbar>
		</el-row>

		<div :class="sidebar.opened && eleTabTop <= 85 ? 'eleTab' : !sidebar.opened && eleTabTop <= 85 ? 'eleTabs' : ''" class="informationTab" ref="eleTab">
			<el-table ref="table" @row-click="row_click" v-loading="loading" :data="equipmentList" @selection-change="handleSelectionChange" border height="100%">
				<el-table-column type="selection" width="55" align="center" />
				<el-table-column label="序号" align="center" type="index" width="55" />
				<el-table-column label="设备名称" align="left" prop="deviceName" min-width="150" :show-overflow-tooltip="true" v-if="columns[0].visible" />
				<el-table-column label="设备编号" align="left" prop="deviceNumber" min-width="150" :show-overflow-tooltip="true" v-if="columns[1].visible" />
				<el-table-column label="设备型号" prop="deviceModel" min-width="150" :show-overflow-tooltip="true" v-if="columns[2].visible" />
				<el-table-column label="精度等级" align="center" prop="accuracyClass" min-width="150" :show-overflow-tooltip="true" v-if="columns[3].visible">
					<template slot-scope="scope">
						<dict-tag :options="dict.type.accuracy_level" :value="scope.row.accuracyClass" />
					</template>
				</el-table-column>
				<el-table-column label="生产厂及国别" prop="manufacturingPlant" min-width="150" :show-overflow-tooltip="true" v-if="columns[4].visible" />
				<el-table-column label="完好状态" prop="intactState" min-width="150" :show-overflow-tooltip="true" align="center" v-if="columns[5].visible">
					<template slot-scope="scope">
						<dict-tag :options="dict.type.measure_intact_state" :value="scope.row.intactState" />
					</template>
				</el-table-column>
				<el-table-column label="生产日期" align="center" prop="producedTime" min-width="150" :show-overflow-tooltip="true" v-if="columns[6].visible">
					<template slot-scope="scope">
						<span>{{ parseTime(scope.row.producedTime, "{y}-{m}-{d}") }}</span>
					</template>
				</el-table-column>
				<el-table-column label="购置日期" align="center" prop="purchaseTime" min-width="150" :show-overflow-tooltip="true" v-if="columns[7].visible">
					<template slot-scope="scope">
						<span>{{ parseTime(scope.row.purchaseTime, "{y}-{m}-{d}") }}</span>
					</template>
				</el-table-column>
				<el-table-column label="价值" prop="cost" min-width="150" :show-overflow-tooltip="true" align="center" v-if="columns[8].visible" />

				<el-table-column label="使用部门" align="center" prop="userDepartment" min-width="150" :show-overflow-tooltip="true" v-if="columns[10].visible" />
				<el-table-column label="检定日期" align="center" prop="lastAppraisalDate" min-width="150" :show-overflow-tooltip="true" v-if="columns[11].visible" />
				<el-table-column label="检定周期" align="center" prop="periodValidity" min-width="150" :show-overflow-tooltip="true" v-if="columns[12].visible">
					<template slot-scope="scope">
						<div v-if="scope.row.periodValidity == 1">一年</div>
						<div v-if="scope.row.periodValidity == 2">半年</div>
						<div v-if="scope.row.periodValidity == 3">一个月</div>
					</template>
				</el-table-column>
        <el-table-column label="有效期" prop="nextAppraisalDate" min-width="150" :show-overflow-tooltip="true" align="center" v-if="columns[15].visible" />
				<!-- <el-table-column label="检定状态" align="center" prop="authenticateStatus" min-width="150" :show-overflow-tooltip="true" v-if="columns[13].visible">
          <template slot-scope="scope">
						<el-tag type="success" v-if="scope.row.authenticateStatus == 1">已检定</el-tag>
						<el-tag type="warning" v-if="scope.row.authenticateStatus == 0">检定中</el-tag>
					</template>
				</el-table-column> -->
				<el-table-column label="送检日期" align="center" prop="inspectDate" min-width="150" :show-overflow-tooltip="true" v-if="columns[14].visible" />
        <el-table-column label="计划送检日期" align="center" prop="planAppraisalDate" min-width="150" :show-overflow-tooltip="true" v-if="columns[13].visible">
					<template slot-scope="scope">
						<span>{{ parseTime(scope.row.planAppraisalDate, "{y}-{m}-{d}") }}</span>
					</template>
				</el-table-column>
				<el-table-column label="检定单位" prop="verificationUnit" min-width="150" :show-overflow-tooltip="true"  v-if="columns[16].visible" />
				<el-table-column label="证书编号" prop="certificateNumber" min-width="150" :show-overflow-tooltip="true"  v-if="columns[17].visible" />
        <el-table-column label="备注" prop="remark" min-width="150" :show-overflow-tooltip="true" v-if="columns[9].visible" />
				<el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="200">
					<template slot-scope="scope">
						<el-button size="mini" type="text" icon="el-icon-edit" @click="handleEdite(scope.row)" v-hasPermi="['quality:qmeasure:edit']">修改</el-button>
						<el-button size="mini" type="text" icon="el-icon-edit" @click="handleJump2(scope.row)" v-hasPermi="['quality:qmeasure:record']">检定记录</el-button>
						<el-button size="mini" type="text" icon="el-icon-wallet" @click="handleJump(scope.row)" v-hasPermi="['quality:qmeasure:annex']">附件</el-button>
					</template>
				</el-table-column>
			</el-table>
		</div>
		<pagination menuNames="Pagination43" v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

		<!-- 添加或修改测量设备对话框 -->
		<el-dialog title="检定计划" :visible.sync="open" width="650px" append-to-body v-dialogDrag>
			<el-form ref="form" class="add-form1" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="记录编号" prop="recordNumber">
					<el-input v-model="form.recordNumber" placeholder="请输入记录编号" maxlength="20" show-word-limit />
				</el-form-item>
				<el-form-item label="送检日期" prop="inspectDate">
					<el-date-picker clearable v-model="form.inspectDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择送检日期"> </el-date-picker>
				</el-form-item>
				<el-form-item label="编号" prop="serialNumber">
					<el-input v-model="form.serialNumber" placeholder="请输入编号" maxlength="20" show-word-limit />
				</el-form-item>

				<el-form-item label="备注" prop="remark" style="width: 100%">
					<el-input v-model="form.remark" type="textarea" placeholder="请输入备注" maxlength="100" show-word-limit />
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>

		<el-dialog title="修改" :visible.sync="openEdit" width="650px" append-to-body v-dialogDrag>
			<el-form ref="editForm" class="add-form" :model="editForm" :rules="rulesEdit" label-width="110px">
				<el-form-item label="检定日期" prop="lastAppraisalDate">
					<el-date-picker clearable v-model="editForm.lastAppraisalDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择日期"> </el-date-picker>
				</el-form-item> 
				<!-- <el-form-item label="有效期" prop="periodValidity">
          <el-input-number v-model="editForm.periodValidity" placeholder="请输入有效期" :min="0" :max="99999999"  />
				</el-form-item> -->
				<el-form-item label="检定周期" prop="periodValidity">
					<el-select v-model="editForm.periodValidity">
						<el-option v-for="item in timelist" :key="item.id" :value="item.id" :label="item.name"> </el-option>
					</el-select>
					<!-- <el-input-number v-model="form.periodValidity" placeholder="请输入有效期" :min="0" :max="99999999" :precision="0" /> -->
				</el-form-item>
        <el-form-item label="计划送检日期" prop="planAppraisalDate">
					<el-date-picker clearable v-model="editForm.planAppraisalDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择计划送检日期"> </el-date-picker>
				</el-form-item>
				<el-form-item label="检定单位" prop="verificationUnit">
					<el-input v-model="editForm.verificationUnit" placeholder="请输入检定单位" maxlength="20" show-word-limit />
				</el-form-item>
				<el-form-item label="证书编号" prop="certificateNumber">
					<el-input v-model="editForm.certificateNumber" placeholder="请输入证书编号" maxlength="20" show-word-limit />
				</el-form-item>
				<el-form-item label="备注" prop="remark" style="width: 100%">
					<el-input v-model="editForm.remark" type="textarea" placeholder="请输入备注" maxlength="100" show-word-limit />
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitEditerForm">确 定</el-button>
				<el-button @click="openEdit = false">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
import { listEquipment1 } from "@/api/equipment/measure/equipment"
import { addPlan, editAuthenticate, getAuthenticate } from "@/api/quality/qmeasure"
import CreateForm from "@/views/components/CreateForm"
import { mapGetters } from "vuex"
import { getcolumn } from "@/api/selectList"
export default {
	name: "Qmeasure",
	dicts: ["accuracy_level", "measure_intact_state", "period_validity", "authenticate_status"],
	components: { CreateForm },

	data() {
		return {
			timelist: [
				{
					id: "1",
					name: "一年"
				},
				{
					id: "2",
					name: "半年"
				},
				{
					id: "3",
					name: "一个月"
				}
			],
			editForm: {},
			openEdit: false,
			eleTabTop: 0, //表格容器距浏览器顶部距离
			// 遮罩层
			loading: true,
			// 选中数组
			ids: [],
			// 非单个禁用
			single: true,
			// 非多个禁用
			multiple: true,
			// 显示搜索条件
			showSearch: true,
			// 总条数
			total: 0,
			// 测量设备表格数据
			equipmentList: [],
			// 弹出层标题
			title: "",
			// 是否显示弹出层
			open: false,
			// 查询参数
			queryParams: {
				pageNum: 1,
				pageSize: 10,
				deviceName: undefined,
				lastAppraisalDate: undefined,
				periodValidity: undefined,
				authenticateStatus: undefined,
				deviceNumber: undefined,
				intactState: undefined,
				purchaseTime: undefined
			},
			// 表单参数
			form: {},
			// 表单校验
			rulesEdit: {},
			rules: {
				recordNumber: [{ required: true, message: "设备名称不能为空", trigger: "blur" }],
				serialNumber: [{ required: true, message: "设备名称不能为空", trigger: "blur" }],
				inspectDate: [{ required: true, message: "设备名称不能为空", trigger: "blur" }]
			},
			// 列信息
			columns: [
				{ key: 0, label: `设备名称`, visible: true },
				{ key: 1, label: `设备编号`, visible: true },
				{ key: 2, label: `设备型号`, visible: true },
				{ key: 3, label: `精度等级`, visible: true },
				{ key: 4, label: `生产厂及国别`, visible: true },
				{ key: 5, label: `完好状态`, visible: true },
				{ key: 6, label: `生产日期`, visible: true },
				{ key: 7, label: `购置日期`, visible: true },
				{ key: 8, label: `价值`, visible: true },
				{ key: 9, label: `备注`, visible: true },
				{ key: 10, label: `使用部门`, visible: true },
				{ key: 11, label: `检定日期`, visible: true },
				{ key: 12, label: `检定周期`, visible: true },
				{ key: 13, label: `计划送检日期`, visible: true },
				{ key: 14, label: `送检日期`, visible: true },
				{ key: 15, label: `有效期`, visible: true },
				{ key: 16, label: `检定单位`, visible: true },
				{ key: 17, label: `证书编号`, visible: true },
			]
		}
	},
	computed: {
		...mapGetters(["sidebar"])
	},
	mounted() {
		// 滚动条的获取
		this.handleScrollx()
		window.addEventListener("scroll", this.handleScrollx, true)
	},
	activated() {
		this.getSize()
		//获取表格容器距页面头部的距离
		this.handleScrollx()
	},
	destroyed() {
		window.removeEventListener("scroll", this.handleScrollx, true)
	},
	created() {
		// this.getSize()
	},
	methods: {
    getSize() {
			getcolumn({ pageName: "Pagination43" }).then(response => {
				this.queryParams.pageSize = response.data.length ? (response.data[0].limit ? response.data[0].limit : 10) : 10
				this.getList()
			})
		},
		handleEdite(row) {
			getAuthenticate(row.id).then(res => {
				this.editForm = res.data
				this.openEdit = true
			})
		},
		// 单击表格行
		row_click(row, column, event) {
			if (column && column.label == "操作") {
				return
			} else {
				this.$refs.table.toggleRowSelection(row)
			}
		},
		changeTableData() {
			this.$nextTick(() => {
				this.$refs.table.doLayout()
			})
		},

		//获取表格容器距页面头部的距离
		handleScrollx() {
			this.eleTabTop = this.$refs.eleTab.getBoundingClientRect().top
		},
		/** 查询测量设备列表 */
		getList() {
			this.loading = true
			listEquipment1(this.queryParams).then(response => {
				this.equipmentList = response.rows
				this.total = response.total
				this.loading = false
			})
		},
		// 取消按钮
		cancel() {
			this.open = false
			this.reset()
		},
		// 表单重置
		reset() {
			this.form = {
				recordNumber: undefined,
				serialNumber: undefined,
				inspectDate: undefined,
				remark: undefined,
        planAppraisalDate: undefined,
			}
			this.resetForm("form")
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.queryParams.pageNum = 1
			this.getList()
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm("queryForm")
			this.handleQuery()
		},
		// 多选框选中数据
		handleSelectionChange(selection) {
			this.ids = selection.map(item => item.id)
			this.single = selection.length !== 1
			this.multiple = !selection.length
		},
		/** 新增按钮操作 */
		handleAdd() {
			if (this.ids.length == 0) {
				this.$modal.msgError("请选择设备")
				return false
			}
			this.reset()
			this.open = true
			this.title = "添加测量设备"
		},
		/** 修改按钮操作 */
		// handleUpdate(row) {
		//   this.reset();
		//   const id = row.id || this.ids;
		//   getEquipment(id).then((response) => {
		//     this.form = response.data;
		//     this.open = true;
		//     this.title = "修改测量设备";
		//   });
		// },

		submitEditerForm() {
			this.$refs["editForm"].validate(valid => {
				if (valid) {
					editAuthenticate(this.editForm).then(response => {
						this.$modal.msgSuccess("操作成功")
						this.openEdit = false
						this.getList()
					})
				}
			})
		},
		/** 提交按钮 */
		submitForm() {
			this.$refs["form"].validate(valid => {
				if (valid) {
					this.form.ids = this.ids
					addPlan(this.form).then(response => {
						this.$modal.msgSuccess("操作成功")
						this.open = false
						this.getList()
					})
				}
			})
		},
		/** 删除按钮操作 */
		// handleDelete(row) {
		//   const ids = row.id || this.ids;
		//   this.$modal
		//     .confirm("是否确认删除测量设备的数据项？")
		//     .then(function () {
		//       return delEquipment(ids);
		//     })
		//     .then(() => {
		//       this.getList();
		//       this.$modal.msgSuccess("删除成功");
		//     })
		//     .catch(() => {});
		// },
		// 跳转到附件
		handleJump(row) {
			this.$router.push({
				path: "/attachment/attachmentUpload",
				query: {
					TableId: row.id,
					typeId: 2,
					perm: "quality:qmeasure:annex"
				}
			})
		},
		// 跳转到检定记录
		handleJump2(row) {
			let url = "/quality/qrecord?id=" + row.id
			this.$router.push(url)
		}
	}
}
</script>
<style lang="scss" scoped></style>
